function getUrlParam(name) {
    let href = window.location.href.toString();
    let stringArr = href.split("\/");
    return stringArr[stringArr.length-1];
}

function setFilename(name) {
    $(".filename").text(name);
}

function setShareTime(time) {
    $(".shareTime").text(time);
}

function setExpireTime(time) {
    $(".expireTime").text(time)
}

function setFileSize(fileSize) {
    $(".fileSize").text(fileSize);
}

function setShareUsername(username) {
    $(".shareUsername").text(username);
}
function setDownloadURL(url) {
    $(".button").on("click", function () {
        window.location.href = url;
    })
}

function render(fileName, shareTime, expireTime, fileSize, username) {
    setFilename(fileName)
    setShareTime(shareTime)
    setExpireTime(expireTime)
    setFileSize(fileSize);
    setShareUsername(username);
}

function downLoadFile() {
    let checkCode = $("#checkCode").val();
    if (checkCode.trim().length === 0 ) {
        alert("请输入验证码");
    }else {
        $.ajax({
            type: "get",
            url: "/s/isCorrectCheckCode/"+getUrlParam()+"/"+checkCode,
            dataType: "json",
            success: function (resp) {
                if (resp.code === 20000) {
                    a = document.createElement("a");
                    a.href = "/s/d/" + getUrlParam() + "/" + checkCode;
                    a.click();
                    console.log(resp);
                }else {
                    alert(resp.msg)
                }
            }
        })
    }
}

$(function (){
    $.ajax({
        type: "get",
        url: "/s/info/"+getUrlParam(),
        dataType: "json",
        success: function (resp) {
            if(resp.code === 20000){
                $(".loading").hide();
                let data = resp.data;
                render(data.shareFileName, data.shareFileTime,
                    data.expiredTime, data.shareFileSize, data.shareUserName);
            }
            else{
                $(".loading").show();
                alert(resp.msg);
            }
        }
    });
});
